package com.example.occlusiondemo;

import android.content.Context;
import android.widget.Toast;

import com.eqgis.eqtool.cfg.LocalConfigManager;
import com.eqgis.eqtool.tmp.BaseParameterManager;
import com.example.occlusiondemo.activity.StartActivity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Objects;

/**
 * 相关路径配置
 */
public class PathConfig {

    public static final String SD_CARD = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();


    /**
     * 数据文件夹根路径
     * 此电脑\MI 9 Transparent Edition\内部存储设备\SuperMap\PipeData
     */
    public static final String PIPE_DATA_PATH = SD_CARD + "/SuperMap/PipeData";
    /**
     * 许可路径
     */
    public static final String LICENSE_PATH = PIPE_DATA_PATH + "/license";
//    public static final String LICENSE_PATH = SD_CARD + "/SuperMap/license";

    /**
     * 日志存储文件夹
     */
    public static final String LOG_PATH = PIPE_DATA_PATH + "/Log";

    /**
     * 图片识别文件夹
     */
    public static final String IMG_DATA_PATH = PIPE_DATA_PATH + "/ImgData";

    /**
     * 屋顶通风管道模型组文件夹路径
     */
    public static String TOP_PIPE_DATA = PIPE_DATA_PATH + "/Model/top_pipe";

    /**
     * 地下管线模型组
     */
    public static String UNDERGROUND_PIPE_DATA = PIPE_DATA_PATH + "/Model/underground_pipe";

    /**
     * 垂直墙内的管线数据
     */
    public static String VERTICAL_PIPE_DATA = PIPE_DATA_PATH + "/Model/pipe_v";

    /**
     * TMP
     */
    public static final String TEST_DATA = SD_CARD+"/SuperMap/pipes/gltf";


    //<editor-fold> - 遮挡纹理
    /**
     * 卷帘模式中，天空包围盒路径
     * <pre>ASSETS下的路径</pre>
     */
    public static final String ROLLING_FUNC_SKYBOX_BG = "test2.png";
//    public static final String ROLLING_FUNC_SKYBOX_BG = "brown_mud_dry_max.png";

    /**
     * 视口模式，开挖的坑洞纹理底部
     */
    public static final String VIEWPORT_FUNC_PIT_OBJ_TEXTURE_A1 = "a_underground03.png";
    public static final String VIEWPORT_FUNC_PIT_OBJ_TEXTURE_A2 = "a_wall03.png";
    public static final String VIEWPORT_FUNC_PIT_OBJ_TEXTURE_A3 = "a_celling03.png";
//    public static final String VIEWPORT_FUNC_PIT_OBJ_TEXTURE_A = "brown_mud_dry2.png";
    /**
     * 视口模式，开挖的坑洞纹理侧面
     */
    public static final String VIEWPORT_FUNC_PIT_OBJ_TEXTURE_B1 = "wall02.png";
    public static final String VIEWPORT_FUNC_PIT_OBJ_TEXTURE_B2 = "wall02.png";
    public static final String VIEWPORT_FUNC_PIT_OBJ_TEXTURE_B3 = "wall02.png";

    /**
     * 墙面高度
     */
    public static float HEIGHT = 5.0f;
    public static float MARKER_WIDTH = 0.13F;

    /**
     * 使用自定义设置
     * @param context
     */
    public static boolean useCustomSetting(Context context) {
        File file = new File(PathConfig.LICENSE_PATH);
        if (!file.exists()){
            file.mkdirs();
        }
        file = new File(PathConfig.IMG_DATA_PATH);
        if (!file.exists()){
            file.mkdirs();
        }
        //若文件不存在，则拷贝文件
        if (!copyToSD(context,"default.cfg",PathConfig.PIPE_DATA_PATH + "/app.cfg"))return false;
        if (!copyToSD(context,"mark.png",PathConfig.IMG_DATA_PATH + "/mark.png"))return false;

        if (!copyToSD(context,"SuperMapiMobileTrial.slm",PathConfig.LICENSE_PATH+"/SuperMapiMobileTrial.slm"))return false;

//        BaseParameterManager instance = BaseParameterManager.getInstance();
        LocalConfigManager instance = LocalConfigManager.getInstance();
        instance.setLocalPath(PathConfig.PIPE_DATA_PATH + "/app.cfg");
        instance.loadConfig(context);
        try {
            TOP_PIPE_DATA = PIPE_DATA_PATH + instance.getResultMap().get("TOP_PIPE_DATA");
            UNDERGROUND_PIPE_DATA = PIPE_DATA_PATH + instance.getResultMap().get("UNDERGROUND_PIPE_DATA");
            VERTICAL_PIPE_DATA = PIPE_DATA_PATH + instance.getResultMap().get("VERTICAL_PIPE_DATA");
            String height = instance.getResultMap().get("HEIGHT");
            if (!("".equals(height))){
                HEIGHT = Float.parseFloat(height);
            }
            String markerWidth = instance.getResultMap().get("MARKER_WIDTH");
            if (!("".equals(height))){
                MARKER_WIDTH = Float.parseFloat(markerWidth);
            }
        }catch (NullPointerException e){
            Log.e(PathConfig.class.getSimpleName(),e.toString());
            return false;
        }
        return true;
    }

//    public static final String VIEWPORT_FUNC_PIT_OBJ_TEXTURE_B = "wall_texture.png";
    //</editor-fold>.


    private static boolean copyToSD(Context context,String src,String dst) {
        File file = new File(dst);
        if (!file.exists()){
            try {
                InputStream is;
                OutputStream os;
                is = context.getAssets().open(src);
                os = new FileOutputStream(file);
                byte[] buffer = new byte[1024];
                int length;
                while ((length = is.read(buffer)) > 0) {
                    os.write(buffer, 0, length);
                }
                is.close();
                os.close();
                Log.i(PathConfig.class.getSimpleName(),"配置-"+src+" 初始化完成");
            } catch (IOException e) {
                Log.e(PathConfig.class.getSimpleName(),e.toString());
                return false;
            }
        }
        return true;
    }
    private static void copyAssetsToDst(Context context, String srcPath, String dstPath) {
        try {
            String fileNames[] = context.getAssets().list(srcPath);
            if (fileNames.length > 0) {
                File file = new File(dstPath);
                if (!file.exists()) file.mkdirs();
                for (String fileName : fileNames) {
                    if (!srcPath.equals("")) { // assets 文件夹下的目录
                        copyAssetsToDst(context, srcPath + File.separator + fileName, dstPath + File.separator + fileName);
                    } else { // assets 文件夹
                        copyAssetsToDst(context, fileName, dstPath + File.separator + fileName);
                    }
                }
            } else {
                File outFile = new File(dstPath);
                InputStream is = context.getAssets().open(srcPath);
                FileOutputStream fos = new FileOutputStream(outFile);
                byte[] buffer = new byte[1024];
                int byteCount;
                while ((byteCount = is.read(buffer)) != -1) {
                    fos.write(buffer, 0, byteCount);
                }
                fos.flush();
                is.close();
                fos.close();
                Log.i(PathConfig.class.getSimpleName(),"配置-"+srcPath+" 初始化完成");
            }
        } catch (Exception e) {
            Log.e(PathConfig.class.getSimpleName(),e.toString());
        }
    }
}
